<?php

namespace App\Api\Helpers\Push;


use App\Helpers\Push\Messages\ChangePasswordMessage;
use Overtrue\EasySms\EasySms;


/**
 *
 * 短信发送工具类
 * Class Sms
 * @package App\Api\Helpers\Push
 */
class Sms
{

    // 发送的人的手机号
    /**
     * @var
     */
    protected $to;

    // 发送的验证码
    /**
     * @var
     */
    protected $code;

    /**
     * @var
     */
    protected $template;

    /**
     * @var mixed
     */
    private $secretKey;
    /**
     * @var mixed
     */
    private $appkey;


    protected $client;


    protected $request;


    public static function make(){

        $instance = new static();

        return $instance->client;

    }


    /**
     * Sms constructor.
     */
    public function __construct()
    {

        $this->appkey = ''.env('SMS_APP_KEY');
        $this->secretKey = ''.env('SMS_APP_SECRET');

        $config = [

            'timeout' => 5.0,
            // 默认发送配置
            'default' => [
                // 默认可用的发送网关
                'gateways' => [
                    'alidayu'
                ]
            ],
            // 可用的网关配置
            'gateways' => [
                'alidayu' => [
                    'app_key' => $this->appkey,
                    'app_secret' => $this->secretKey,
                    'sign_name' => '言士创作',
                ],
            ]
        ];

//        dd($config);

        $easySms = new EasySms($config);

        $this->client = $easySms;

//        return;

//        try{
//
//            $message = new ChangePasswordMessage(2222);
//            $easySms->send('17512575939',$message);
//
//        }catch (\Overtrue\EasySms\Exceptions\Exception $e){
//
//            dd($e);
//
//        }







//        $client->secretKey = $this->secretKey;
//        $client->appkey = $this->appkey;
//
//        $this->client = $client;
//
//        $this->request = $this->getRequest();

    }


    /**
     * @return \AlibabaAliqinFcSmsNumSendRequest
     */
    private function getRequest(){

        $request = new \AlibabaAliqinFcSmsNumSendRequest();

        $request ->setExtend( "" );
        $request ->setSmsType( "normal" );
        $request ->setSmsFreeSignName( "言士创作" );

        return $request;

    }


    /**
     * @param $to
     * @return $this
     */
    public function setTo($to)
    {
        $this->to = $to;

        // 设置发送的人的电话
        $this->request->setRecNum($to);

        return $this;

    }


    /**
     * @param $code
     * @return $this
     */
    public function setCode($code)
    {
        $this->code = $code;

        $this->request->setSmsParam("{code:'".$code."',product:'言士创作'}");

        return $this;
    }

    /**
     * @param $template
     * @return $this
     */
    public function setTemplate($template)
    {
        $this->template = $template;

        $this->request->setSmsTemplateCode($template);

        return $this;

    }




    /**
     * 发送短信验证码
     */
    public function send(){

        $result = $this->client->execute($this->request);

        return new SmsSendResult($result);

    }



}

